const query = require('../config/db.config.js')

const adminMod = {
  validateAccMOd: async (data) => {
    const { acc } = data
    let sql = `
      SELECT COUNT(*) total FROM admin where acc="${acc}"`
      console.log(sql);
    const res = await query(sql)
    return res[0].total
  },
  addAdmMod: async (data) => {
    const {name ,acc ,pwd } =  data
    let createSql =
      `INSERT INTO admin 
      (name,acc,pwd) 
    VALUES 
      ("${name}", "${acc}", "${pwd}")`
    await query(createSql)
  },
  delelteAdmMod: async (data) => {
    const {admID } =  data
    console.log(admID);
    let sql = `DELETE FROM admin  where  admID = "${admID}"`
    await query(sql)
  },
  editpwdWebMOd : async (data) =>{
    const {admID,pwd} = data
    let sql = `UPDATE admin 
    SET
    pwd = "${pwd}"
    where admID = "${admID}"
  `
    await query(sql)
  },
  selectAdmMod: async (data) =>{
    const { name ,acc , limit , page} =  data
    let pageCurr = (page -1)* limit
    let occ = ''
    let sql = 
    `SELECT  
      *
    FROM 
      admin ad
    where 
     (ad.name LIKE '%${ name || occ }%' )
     AND  (ad.acc LIKE '%${ acc || occ }%' )
       LIMIT ${ pageCurr },${ limit }
    `
console.log(sql);
   let sql2 = `SELECT COUNT(*) total FROM admin ;`
    const total = await query(sql2)
    const res = await query(sql)
    return {
      total : total[0].total,
      list : res
    }
  },
 
}

module.exports = adminMod